//$Id: UnitTestingAI.java 233 2010-11-08 04:52:42Z liangyj20030161@yahoo.com.sg $
package builders;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * This class acts as an interface to do ATD.
 * It calls on methods in aiTestCase to do the *real* computation and comparisons.
 * @author Liang Yujing
 *
 */
public class UnitTestingAI {
	String inputFile;
	String expectedOutputFile;
	String resultsFile;
	ArrayList<aiTestCase> testCases = new ArrayList<aiTestCase>();

	/**
	 * Constructor that takes in test cases, expected results, and an empty results file to begin with
	 * @param inputFile
	 * @param expectedOutputFile
	 * @param resultsFile
	 */
	public UnitTestingAI(String inputFile, String expectedOutputFile, String resultsFile){
		this.inputFile = inputFile;
		this.expectedOutputFile = expectedOutputFile;
		this.resultsFile = resultsFile;
	}
	
	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String workingDir = System.getProperty("user.dir");
		String inputFile = workingDir + "\\src\\builders\\aitestcases.txt";
		String expectedOutputFile = workingDir + "\\src\\builders\\aiexpectedoutput.txt";
		String outputFile = workingDir + "\\src\\builders\\aitestresults.txt";
		UnitTestingAI unitTester = new UnitTestingAI(inputFile, expectedOutputFile, outputFile);
		unitTester.readTestCases();
		unitTester.executeTests();
		unitTester.writeResults();
	}
	
	/**
	 * Reads in test cases, converts them into aiTestCase and add the aiTestCases into a private
	 * ArrayList<aiTestCase>
	 * Method made public for SystemTesting
	 */
	public void readTestCases() {
		File input = new File(inputFile);
		File expectedOutput = new File(expectedOutputFile);
		
		try {
			FileReader inputFileReader = new FileReader(input);
	        BufferedReader in = new BufferedReader (inputFileReader);
	        
	    	FileReader outputFileReader = new FileReader(expectedOutput);
	        BufferedReader out = new BufferedReader (outputFileReader);
	        
	        String tilesYouCanMoveTo = null;
	        String buildingsYouCanBuild = null;
	        String buildingAvailableForBuilding1 = null;
	        String buildingAvailableForBuilding2 = null;
	        String buildingAvailableForBuilding3 = null;
	        String buildingAvailableForBuilding4 = null;
	        String dieValue = null;
	        String currentResourceAmount = null;
	        String command = null;
	        String lineFromOutput = null;
	        try {
				while ((tilesYouCanMoveTo = in.readLine()) != null){
					if ((buildingsYouCanBuild = in.readLine()) == null ||
							(buildingAvailableForBuilding1 = in.readLine()) == null ||
							(buildingAvailableForBuilding2 = in.readLine()) == null ||
							(buildingAvailableForBuilding3 = in.readLine()) == null ||
							(buildingAvailableForBuilding4 = in.readLine()) == null ||
							(dieValue = in.readLine()) == null ||
							(currentResourceAmount = in.readLine()) == null ||
							(command = in.readLine()) == null){
						System.out.println("Check conditions for input file.");
					}

					if ((lineFromOutput = out.readLine())== null){
						System.out.println("Number of test cases and expected output do not match.");
					}
					
					aiTestCase tc = new aiTestCase(tilesYouCanMoveTo, buildingsYouCanBuild, buildingAvailableForBuilding1, buildingAvailableForBuilding2, buildingAvailableForBuilding3, buildingAvailableForBuilding4, dieValue, currentResourceAmount, command, lineFromOutput);
					testCases.add(tc);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}	
	
	/**
	 * Executes the aiTestCases
	 */
	public void executeTests() {
		for (aiTestCase tc : testCases){
			tc.verifyResult();
		}
	}
	
	/**
	 * Gets results from aiTestCases and outputs them into results file
	 */
	public void writeResults() {
		int numOfCorrectCases = 0;
		int numOfWrongCases = 0;
		int testCaseNum = 1;
		
		try {
		    BufferedWriter out = new BufferedWriter(new FileWriter(resultsFile));
		    for (aiTestCase tc : testCases){
		    	boolean correct = tc.getResult();
		    	if (correct){
		    		out.write("Test case " + testCaseNum + ": True\n");
		    		out.write("*****\n");
		    		testCaseNum++;
		    		numOfCorrectCases++;
		    	} else {
		    		out.write("Test case " + testCaseNum + ": False\n");
		    		testCaseNum++;
		    		numOfWrongCases++;
		    		
		    		out.write("This is your output: " + tc.getOuput() + "\n");
		    		out.write("This is the expected output: " + tc.getExpectedOutput() + "\n");
		    		out.write("*****\n");
		    	}
		   }
		    out.write("Total number of passed cases: " + numOfCorrectCases + "\n");
		    out.write("Total number of failed cases: " + numOfWrongCases + "\n");
		    out.close();
		} catch (IOException e) {
		}
		System.out.println("AI tested.");
	}
	
}
